var express = require('express');
const { api_url } = require('../utils/config.js');
var router = express.Router();
const Quert = require('../utils/db.js')

router.get('/',async function(req, res, next) {
    res.send('课程')
});

/*

-- 1/、拿到课程详细信息，里面有tid字段（代表这个课程是哪个老师讲授的）  1beec178-4b02-40fb-9d1c-8aa61cd4a097
SELECT * FROM e_course WHERE cid='1beec178-4b02-40fb-9d1c-8aa61cd4a097'

-- 2、t10001 此老师的id编号
SELECT * FROM e_teacher WHERE tid='t10003'

-- 3、根据cid去e_video表中查询此课程有几节内容（几个视频）
SELECT * FROM e_video WHERE cid='1beec178-4b02-40fb-9d1c-8aa61cd4a097'

*/ 
router.get('/course',async (req,res,next)=>{
    let {cid} = req.query
    let sql1 = `SELECT *,CONCAT ('${api_url}','/uploads/courses/',image_src) AS image_src FROM e_course WHERE cid='${cid}'`
    let {err,result} = await Quert(sql1)
    if(err){
        next({
            "msg": "1出错了哦",
            "status": 500,
        })
        return
    }

    let tid = result[0].tid
    console.log(tid);

    let sql2 = `SELECT *,CONCAT ('${api_url}','/uploads/head/',head_photo_url) AS head_photo_url FROM e_teacher WHERE tid='${tid}'`
    let res2 = await Quert(sql2)
    if(res2.err){
        next({
            "msg": "2出错了哦",
            "status": 500,
        })
        return
    }
    let sql3 = `SELECT id,cid,vnum,video_title FROM e_video WHERE cid='1beec178-4b02-40fb-9d1c-8aa61cd4a097'`
    let res3 = await Quert(sql3)
    if(res3.err){
        next({
            "msg": "3出错了哦",
            "status": 500,
        })
        return
    }
    let data = {
        msg: "ok",
        status: 200,
        result:{
            course:result[0],
            teacher:res2.result[0],
            videos:res3.result
        }
    }
    res.send(data)
})

module.exports = router;
